package com.tedu.ds.dept.service.impl;

import com.tedu.ds.dept.mapper.DepartmentMapper;
import com.tedu.ds.dept.pojo.dto.DepartmentParam;
import com.tedu.ds.dept.pojo.dto.DepartmentQuery;
import com.tedu.ds.dept.pojo.entity.Department;
import com.tedu.ds.dept.pojo.vo.DepartmentVO;
import com.tedu.ds.dept.service.DepartmentService;
import com.tedu.ds.employee.mapper.EmployeeMapper;
import com.tedu.ds.employee.pojo.dto.EmployeeQuery;
import com.tedu.ds.employee.pojo.vo.EmployeeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<DepartmentVO> select(DepartmentQuery departmentQuery) {
        List<DepartmentVO> departmentVOList=departmentMapper.select(departmentQuery);
        for (DepartmentVO d:departmentVOList){
            if (d.getManagerId()!=null){
                EmployeeQuery employeeQuery=new EmployeeQuery();
                employeeQuery.setId(d.getManagerId());
                List<EmployeeVO> employeeVOList=employeeMapper.select(employeeQuery);
                if (!employeeVOList.isEmpty()){
                    d.setManagerName(employeeVOList.get(0).getUsername());
                }

            }
        }
        return departmentVOList;
    }

    @Override
    public void save(DepartmentParam departmentParam) {
        Department department=new Department();
        BeanUtils.copyProperties(departmentParam,department);
        if (department.getId()==null){
            department.setStatus("20");
            departmentMapper.insert(department);
        }else {
            departmentMapper.update(department);
        }
    }

    @Override
    public void deleteById(Integer id) {
        departmentMapper.deleteById(id);
    }
}
